package com.zjc;

import java.io.Closeable;
import java.io.IOException;
import java.net.Socket;

public class Connect implements Closeable {
    public long timestamp;
    public Socket source;
    public Socket target;

    public Connect(Socket source, ForwardMsg msg) throws IOException {
        this.timestamp = System.currentTimeMillis();
        this.source = source;
        this.target = msg.targetUri == null ? null : new Socket(msg.targetUri, msg.targetPort);
    }

    @Override
    public void close() throws IOException {
        if (this.target != null) {
            this.target.close();
        }
        if (this.source != null) {
            this.source.close();
        }
    }

    @Override
    public String toString() {
        return "请求转发: " + source.toString() + " --> " + (target == null ? "请求指定" : target.toString());
    }
}
